<?php

require_once 'db.php';
require_once 'log4php/Logger.php';
require_once 'error.php';
require_once 'ex.php';


/**
 * Verifies if the user denoted by uid is already present in the database.
 * @param db reference to an open database connection.
 * @param uid User identifier.
 * @throw PODExceptiion if something wrong happens during the interaction with the db
 * @return true if the user has been already inserted in the database, false otherwise.
 */
function checkSonnaUser($id){
	$stmt = DBManager::statementQuerySonnaUserById();
	$stmt->bindValue(':fId', $id, PDO::PARAM_STR);
	$stmt->execute();
	$rows = count($stmt->fetchAll());
	$result = $rows > 0;
	$stmt->closeCursor();

	return $result;
}
/**
 * Verifies if the user denoted by uid is already present in the database.
 * @param db reference to an open database connection.
 * @param uid User identifier.
 * @throw PODExceptiion if something wrong happens during the interaction with the db
 * @return true if the user has been already inserted in the database, false otherwise.
 */
function checkUser($id){
	$stmt = DBManager::statementQueryUserById();
	$stmt->bindValue(':place', $id, PDO::PARAM_STR);
	$stmt->execute();
	$rows = count($stmt->fetchAll());
	$result = $rows > 0;
	$stmt->closeCursor();

	return $result;
}

/**
 * Retrieves the user using the facebook id from the database.
 * @param id User identifier.
 * @throw PODExceptiion if something wrong happens during the interaction with the db
 * @return an associative array filled with the user profile or null if
 * there is not a user with the given id in the DB.
 */
function getUser($id){
	$stmt = DBManager::statementQueryUserById();
	$stmt->bindValue(':place', $id, PDO::PARAM_STR);
	if(!$stmt->execute())
		throw new DBException('Error while executing query in getUser');

	$user = $stmt->fetch(PDO::FETCH_ASSOC);
	if($user == false)
		return null;
//		throw new DBException('Error while fetching rows in getUser');

	$stmt->closeCursor();
	return $user;
}

/**
 * Returns a Logger instance.
 * If a Logger have never been created it first creates a new instance
 */
function getLogger(){
	static $log;
	if(is_null($log)){
		Logger::configure('log4php.xml');
		$log = Logger::getLogger('db');
	}
	return $log;
}

/**
 * Creates a new error using the information contained in the JSON parameter.
 * @param j A JSON object containing the error information.
 * @return a new error object filled with the information contained in the 
 * provided JSON object.
 */
function getErrorFromJSON($j){
	return new Error(
		$j['message'],
		$j['location'],
		!empty($j['id']) ? $j['id'] : -1,
		!empty($j['level']) ? $j['level'] : Error::LVL_ERROR
	);
}

/**
 * Creates a new error to notify an empty payload in a rest request.
 * @param loc The location where the error happened.
 */
function emptyPayloadError($loc){
 	$e = new Error('La richiesta non ha alcun parametro.');
	$e->setLocation($loc);
	return $e;
}

function getFBAppID(){
	return '129522767159878';
}

function getFBAppSecret(){
	return 'aa2747c91d2d60f6e55991e144352e9c'; 
}

function getFBUrlExtendedToken($token){
	return 'https://graph.facebook.com/oauth/access_token?'
		.'client_id=' . getFBAppID()
		.'&client_secret=' . getFBAppSecret()
		.'&grant_type=fb_exchange_token'
		.'&fb_exchange_token=' . $token;
}

/**
* Returs the string value of the array element indexed by key or null 
* if such a key does not exist.
*/
function getArrayValue($array, $key){
	if(!array_key_exists($key, $array))
		return null;
	if(is_array($array[$key]))
		return json_encode($array[$key]);

	return $array[$key];
}
?>
